package com.hdoit.ares.logical.advertisement.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;
import com.hdoit.ares.logical.advertisement.dao.IAdvertisementDao;
import com.hdoit.ares.logical.advertisement.model.Advertisement;
import com.hdoit.ares.logical.advertisement.model.AdvertisementLocation;
import com.hdoit.ares.logical.viewmodel.advertisement.AdvertisementSearch;
/**
 * @author      yujinhui
 * @email       
 * @createtime  2018年03月13日 19:25:11
 * @comment
 */
@Component
public class AdvertisementDaoImpl extends SuperDao<Advertisement> implements IAdvertisementDao{

	@Override
	public List<Advertisement> queryList(AdvertisementSearch search, PagerModel pagerModel) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		
		finderExpression.select("advertisement.*","advertisementLocation.name as locationName")
						.from(Advertisement.class, "advertisement")
						.leftJoin(AdvertisementLocation.class, "advertisementLocation")
						.on("advertisement.adLocationId","advertisementLocation.id");
		
		appendFinder(search,finderExpression);

        if(pagerModel == null){
            return queryList(finderExpression, search); 
          }
        return queryList(finderExpression, search, pagerModel);
	}

	private void appendFinder(AdvertisementSearch search, IFinderExpression finderExpression) {
		if(!StringUtils.isEmpty(search.getAdLocationId())){
			finderExpression.andWhere("advertisement.adLocationId = #{adLocationId}")
							.setParam("adLocationId", search.getAdLocationId());
		}
		
		if(!StringUtils.isEmpty(search.getName())){
			finderExpression.andWhere("advertisement.name like #{name}")
			  				.setParam("name", "%"+search.getName()+"%");
		}
		
		finderExpression.addOrder("advertisement.createTime", OrderEnum.desc);
		
	}

	@Override
	public List<Advertisement> queryList(AdvertisementSearch search) {
		return queryList(search,null);
	}

	@Override
	public int queryCount(AdvertisementSearch search) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		
		finderExpression.select("count(advertisement.id) as count")
						.from(Advertisement.class, "advertisement")
						.leftJoin(AdvertisementLocation.class, "advertisementLocation")
						.on("advertisement.adLocationId","advertisementLocation.id ");
		
		appendFinder(search,finderExpression);
		
		return queryCount(finderExpression, search);
	}

	@Override
	public Advertisement queryModelByAdName(String adName) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		
		finderExpression.select("*")
						.from(Advertisement.class)
						.andWhere("adName = #{adName}")
						.setParam("adName", adName);
	
		return queryModel(finderExpression);
	}
	
	
}